<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div style="width:350px; margin:0px auto; margin-top:150px;">
	<form class="layui-form" action="">
		<input type="hidden" value="${spuInfo.id}" name ="id"/>
		
		<div style="width:300px;" class="layui-form-item">
		   	<label class="layui-form-label">名称</label>
		    <div class="layui-input-block">
		      	<input type="text" value="${spuInfo.name}" name="name" lay-verify="title" autocomplete="off" placeholder="请输入名称" class="layui-input">
		    </div>
  		 </div>
	  	<div class="layui-form-item">
	      <label class="layui-form-label">分类</label>
	      <div class="layui-input-inline">
	        <select id="cateId" name="cateId" lay-verify="required" lay-search="">
	        </select>
	      </div>
	    </div>
	    <div class="layui-form-item">
	      <label class="layui-form-label">品牌</label>
	      <div class="layui-input-inline">
	        <select id="brandId" name="brandId" lay-verify="required" lay-search="">
	        </select>
	      </div>

	    </div>
	    
	    <div class="layui-form-item">
	      <label class="layui-form-label">状态</label>
	      <div class="layui-input-inline">
	        <select name="status" id="status" lay-verify="required" lay-search="">
	        </select>
	      </div>
	    </div>
	    <div class="layui-form-item">
	      <label class="layui-form-label">店铺</label>
	      <div class="layui-input-inline">
	        <select id="shopId" name="shopId" lay-verify="required" lay-search="">
	        </select>
	      </div>
	    </div>
	     <button style="width:240px; margin-left:60px;" type="button" class="layui-btn com">确认</button>
 	 </div>
	</form>
</div>
</body>
	<script type="text/javascript">
	//加载分类
	function loadCate(){
		$.ajax({
			   type: "get",
			   url: "/pro/allCate",
			   async: false,
			   success: function(data){
				   var $cate = $("#cateId");
				   $cate.html("");
				   $(data).each(function(i,n){
					   if(${spuInfo.cateId}+'' == n.id){
						   $cate.append("<option selected value="+n.id+">"+n.name+"</option>");
					   }else{
						   $cate.append("<option value="+n.id+">"+n.name+"</option>");
					   }
				   })
			   }
			});
	}
	//加载状态
	function loadStatus(){
		var $status = $("#status");
		$status.html("");
		if(${spuInfo.status}+'' == 1){
			$status.append("<option selected value='1'>上架</option>");
			$status.append("<option value='0'>下架</option>");
		}else{
			if(${spuInfo.brandId}+''== 0){
				$status.append("<option selected value='1'>上架</option>");
				$status.append("<option value='0'>下架</option>");
			}else{
				$status.append("<option value='1'>上架</option>");
				$status.append("<option selected value='0'>下架</option>");
			}
			
		}
	}
	//加载品牌
	function loadBrand(){
		$.ajax({
			   type: "get",
			   url: "/pro/allBrand",
			   async: false,
			   success: function(data){
				   var $brand = $("#brandId");
				   $brand.html("");
				   $(data).each(function(i,n){
					   if(${spuInfo.brandId}+'' == n.id){
						   $brand.append("<option  selected value="+n.id+">"+n.name+"</option>");
					   }else{
						   $brand.append("<option value="+n.id+">"+n.name+"</option>");
					   }
				   })
			   }
			});
	}
	//加载店铺
	function loadShop(){
		$.ajax({
			   type: "get",
			   url: "/pro/allShop",
			   async: false,
			   success: function(data){
				   var $shop = $("#shopId");
				   $shop.html("");
				   $(data).each(function(i,n){
					   if(${spuInfo.shopId}+'' == n.id){
						   $shop.append("<option selected value="+n.id+">"+n.name+"</option>");
					   }else{
						   $shop.append("<option value="+n.id+">"+n.name+"</option>");
					   }
					  
				   })
			   }
			});
	}
	layui.use(['table','form'], function(){
		 var table = layui.table
		 ,form = layui.form;
		 //分类
		 loadCate();
		 //品牌
		 loadBrand();
		 //店铺
		 loadShop();
		 loadStatus();
		 
		 form.render();
	})
	</script>
	<script type="text/javascript">
		$(function(){
			$(".com").click(function(){
				//发送ajax请求修改
				$.ajax({
		  			   type: "get",
		  			   url: "/pro/${url}",
		  			   async: false,
		  			   data:$(".layui-form").serialize(),
		  			   success:function(data){
		  				 if(data){
		  					$('.layui-body').load('/pro/toSpu');
		  				 }else{
		  					 layer.open({
			  					  type: 1, 
			  					  content: '<div style="padding: 20px 100px;">'+ '操作失败' +'</div>' //这里content是一个普通的String
		  					 });
		  				  }
		  			   }
				})
				
			})
		})
	</script>
</html>